import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCounterStore = defineStore('counter', () => {
  let carts=ref([])
  let id=ref(0)
  function addcart(detaildata,detailgoodsid){
     let username=localStorage.getItem('username')
     let userCart=carts.value.filter(item=>item.username==username)
     let findcart=userCart.find(item=>item.goods.goods_id==detailgoodsid)
     if(findcart){
        findcart.number++
     }else{
        carts.value.push({
            username:username,
            id:id.value++,
            number:1,
            checked:false,
            goods:detaildata
        })
     }
  }
  function delcart(id){
    carts.value=carts.value.filter(item=>item.id!=id)
  }

  return { carts, addcart, delcart }
},
//持久化存储
{
    persist: true,
  },
)
